﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.ServiceModel;

namespace CodePlexNewReleaseChecker
{
    public class Release
    {
        public string ProjectName { get; private set; }

        public string Name { get { return _baseItem.Name; } }
        public string ChangesetID { get { return _cpRelease.ChangesetId; } }
        public bool DefaultRelease { get { return _cpRelease.DefaultRelease; } }
        public string Description { get { return _cpRelease.Description; } }
        public string DevelopmentStatus { get { return _cpRelease.DevelopmentStatus; } }
        public int ReleaseID { get { return _cpRelease.Id; } }
        public bool IsArchived { get { return _cpRelease.IsArchived; } }
        public bool IsClickOnceRelease { get { return _cpRelease.IsClickOnceRelease; } }
        public string ModifiedByUserName { get { return _cpRelease.ModifiedByUserName; } }
        public DateTime? ModifedDate { get { return _cpRelease.ModifiedDate; } }
        public DateTime? ReleaseDate { get { return _cpRelease.ReleaseDate; } }
        public bool ShowToPublic { get { return _cpRelease.ShowToPublic; } }
        public string Link { get { return _baseItem.Link; } }
        
        private BareRelease _baseItem;
        private CodePlexReleaseService.Release _cpRelease;

        internal Release(string projectName, BareRelease bareItem)
        {
            this.ProjectName = projectName;
            _baseItem = bareItem;

            //load release info
            LoadCPRelease();
        }

        private void LoadCPRelease()
        {
            try
            {
                BasicHttpBinding binding = new BasicHttpBinding(BasicHttpSecurityMode.Transport);

                CodePlexReleaseService.ReleaseServiceSoapClient rssc =
                    new CodePlexReleaseService.ReleaseServiceSoapClient(binding, new EndpointAddress("https://www.codeplex.com/Services/ReleaseService.asmx"));

                if (string.IsNullOrEmpty(_baseItem.Name)) return;

                _cpRelease = rssc.GetRelease(ProjectName, _baseItem.Name, "", "");
            }
            catch { }
        }
    }
}
